/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package cn.ac.iie.di.ban.ycl.bk.e2p.ysyb.tools.fastutil;

import cn.ac.iie.di.ban.ycl.bk.e2p.ysyb.main.BanBkE2p;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.UUID;
import breviary.VideoThumbTaker;

/**
 *
 * @author wmr
 */
public class FileManagerUtil {

    private static org.apache.log4j.Logger logger = org.apache.log4j.Logger.getLogger(FileManagerUtil.class);

    public FastPo upload(String localFilePath) {//上传文件
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//设置日期格式
        logger.info(" FileManagerUtil start " + df.format(new Date()));
        String localPangPath = localFilePath.substring(0, localFilePath.lastIndexOf("/") + 1) + "picture/";
//        File dataF = new File(localFilePath);
//        String fileName = dataF.getName();
        File newPangPath = new File(localPangPath);
        //判断文件夹是否存在 
        if (newPangPath != null && !newPangPath.exists()) {
            newPangPath.mkdirs();
        }
        String uuid = UUID.randomUUID().toString().replace("-", "");
        String pangFilePath = localPangPath + uuid + ".png";
//        String ffmpegPath = "/usr/local/ffmpeg/bin/ffmpeg";
        String ffmpegPath = BanBkE2p.ffmpegPath;
        VideoThumbTaker videoThumbTaker = new VideoThumbTaker(ffmpegPath);
        try {
            logger.info("localFilePath  ---------------" + localFilePath);
            logger.info("pangFilePath  ---------------" + pangFilePath);
            videoThumbTaker.getThumb(localFilePath, pangFilePath, 176, 144, 0, 0, 1);
            logger.info(localFilePath + "create picture success !!!");
        } catch (Exception e) {
            e.printStackTrace();
            logger.error(localFilePath + "create picture failed !!!" + e.getMessage(), e);
        }

        File pangF = new File(pangFilePath);
        boolean sizeFlag = false;
        if (pangF.exists() && pangF.isFile()) {
            logger.info(pangF.length());
            long pangSize = pangF.length();
            if (pangSize == 0) {
                sizeFlag = false;
            } else {
                sizeFlag = true;
            }
        } else {
            logger.error("file pangFilePath doesn't exist or is not a file");
        }

        boolean flag = true;
        FastPo fastPo = new FastPo();
        if (sizeFlag) {
            FastDFSFile dataFile = getFile(localFilePath);
//            FastDFSFile pangFile = getFile(pangFilePath);
//            logger.info("dataFile ---- : " + dataFile);
//            logger.info("pangFile ---- : " + pangFile);
            
            
            if (dataFile == null) {
                flag = false;
                fastPo.setUpFlag(flag);
                fastPo.setFilePath("null");
                fastPo.setPangPath("null");
                logger.error(" FileManagerUtil getfile error -----------------------");
            } else {
                FastDFSFile pangFile = getFile(pangFilePath);
                fastPo = FileManager.uploadVideo(dataFile, pangFile);
            }
        } else {
            FastDFSFile dataFile = getFile(localFilePath);
            if (dataFile == null) {
                flag = false;
                fastPo.setUpFlag(flag);
                fastPo.setFilePath("null");
                fastPo.setPangPath("null");
                logger.error(" FileManagerUtil getfile error -----------------------");
            } else {
                fastPo = FileManager.upload(dataFile);
            }
        }

        return fastPo;
    }

    public FastDFSFile getFile(String localFilePath) {
//        FileInputStream fis = null;
        FastDFSFile file = null;
        logger.info("getFile start  -------" + localFilePath);
        try {
//            boolean flag = true;
            File content = new File(localFilePath);
//            fis = new FileInputStream(content);
            byte[] file_buff = null;
//            if (fis != null) {
//                int len = fis.available();
//                file_buff = new byte[len];
//                fis.read(file_buff);
//            }
            String fileName = content.getName();
            int idx = fileName.lastIndexOf(".");
            String ext = "";
            if (idx <= 0) {
            } else {
                ext = fileName.substring(idx + 1);
            }
//            fis.close();
//            file = new FastDFSFile(idx <= 0 ? fileName : fileName.substring(0, idx), file_buff, ext);
            file = new FastDFSFile(localFilePath, file_buff, ext);
        } catch (Exception ex) {
            file = null;
            logger.error(" FileManagerUtil Exception error --------- " + ex.getMessage(), ex);
//            return file;
        } finally {
            return file;
//            try {
//                fis.close();
//                return file;
//            } catch (IOException ex) {
//                file = null;
//                logger.error(" FileManagerUtil fis close error --------- " + ex.getMessage(), ex);
//                return file;
//            }
        }

    }

    public void delFile(String strGroup, String strPath) throws Exception {
        FileManager.deleteFile(strGroup, strPath);
        logger.info("delete fast file -------------------------");
    }

    public static void main(String[] args) {
        try {
            FileManagerUtil testFileManager = new FileManagerUtil();
            testFileManager.upload("D:\\520.txt");
        } catch (Exception ex) {
//            Logger.getLogger(TestFileManager.class.getName()).log(Level.SEVERE, null, ex);
            System.out.println("error----------------------" + "\r\n" + ex);
        }
    }
}
